<?php

require_once("database_api.php");
require_once("defines_inc.php");


function is_active_user_session()
{
	if(isset($_SESSION["user_id"]) && isset($_SESSION["app"]))
		return $_SESSION["app"] == "share";
}

class user_api
{
	private $db;
	
	public function __construct($db) 
	{
		$this->db = $db;
    }
	
	/* user gate */
	
	public function get_user_id($email)
	{
		$sql =  $this->db->safeQuery("SELECT USR_ID FROM users WHERE USR_EMAIL = '{0}' AND USR_STATUS > {1} LIMIT 1", $email, USER_STATUS_NONE);
		
		if($result = $this->db->query($sql))
		{
			$row = $result->fetch_assoc();
			$result->free();
		
			if($row)
			{
				return $row["USR_ID"];
			}
		}
				
		return 0;
	}
	
	public function validate_new_user($email)
	{
		if($this->get_user_id($email) > 0) return false;
		
		return true;
	}
	
	public function create_user($name, $password, $email)
	{
		$sql =  $this->db->safeQuery("INSERT INTO users(USR_NAME, USR_PASSWORD, USR_EMAIL, USR_STATUS, USR_DATE_REGISTER, USR_DATE_LOGIN)"
		. " VALUES('{0}', '{1}', '{2}', {3}, now(), now())", trim($name), md5(trim($password)), trim($email), USER_STATUS_NONE);
		
		return $this->db->query($sql) === TRUE;
	}		
	
	public function login_user($email, $password)
	{
		$sql =  $this->db->safeQuery("SELECT USR_ID, USR_STATUS FROM users WHERE USR_EMAIL = '{0}' AND USR_PASSWORD = '{1}' LIMIT 1", 
			trim($email), md5(trim($password)));		
		
		if($result = $this->db->query($sql))
		{
			$row = $result->fetch_assoc();
			$result->free();
			
			$sql = $this->db->safeQuery("UPDATE users SET USR_DATE_LOGIN = now() WHERE USR_ID = {0}", $row["USR_ID"]);
			$this->db->query($sql);
			
			return array($row["USR_ID"], $row["USR_STATUS"]);
		}
		
		return 0;
	}
	
	public function logout_user()
	{
		unset($_SESSION);
		session_destroy();
	}
	
	/* user data */
	
	public function set_user_status($user_id, $status)
	{
		$sql = $this->db->safeQuery("UPDATE users SET USR_STATUS = {0} WHERE USR_ID = {1}", $status, $user_id);
		return $this->db->query($sql) === TRUE;
	}
	
	public function set_user_password($user_id, $password)
	{
		$sql = $this->db->safeQuery("UPDATE users SET USR_PASSWORD = '{0}' WHERE USR_ID = {1}", trim($password), $user_id);
		return $this->db->query($sql) === TRUE;
	}
	
	public function get_user_data($user_id)
	{
		$sql = $this->db->safeQuery("SELECT USR_NAME FROM users WHERE USR_ID = {0} LIMIT 1", $user_id);
		
		if($result = $this->db->query($sql))
		{
			$row = $result->fetch_assoc();
			$result->free();
			
			return $row;
		}
		
		return 0;
	}
	
	/* other */	
	
	public function generate_hash_code($email)
	{
		return md5($email);
	}
	
	/* subscriber */
	
	public function add_subscriber($user_id, $subscriber_id)
	{
		$sql =  $this->db->safeQuery("INSERT INTO subscribers (SBC_USR_ID, SBC_SUB_USR_ID) VALUES({0}, {1})", $user_id, $subscriber_id);
		return $this->db->query($sql) === TRUE;
	}
	
	public function remove_subscriber($user_id, $subscriber_id)
	{
		$sql =  $this->db->safeQuery("DELETE FROM subscribers WHERE SBC_USR_ID = {0} AND SBC_SUB_USR_ID = {1}", $user_id, $subscriber_id);
		return $this->db->query($sql) === TRUE;
	}
	
	public function is_subscriber($user_id, $subscriber_id)
	{
		$sql =  $this->db->safeQuery("SELECT SBC_ID FROM subscribers WHERE SBC_USR_ID = {0} AND SBC_SUB_USR_ID = {1} LIMIT 1", $user_id, $subscriber_id);
		
		if($result = $this->db->query($sql))
		{
			$row = $result->fetch_assoc();
			$result->free();
			
			if($row)
			{
				return true;
			}
		}
		
		return false;
	}
	
	public function add_profile_image($user_id)
	{
	}
}

?>